/**
 * The file for the Universe class.
 */

package edu.gatech.information;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;

/**
 * The Class Universe.
 * @author Hippie Mind Control
 * @version 1.0
 */
public class Universe {
	
	/** The planet count. */
	private int planetCount = 6;
	
	/** The names of the planets */
	private String[] planetNames;
	
	/** The planets */
	private Planet[] planetArray;
	
	/**
	 * Instantiates a new universe.
	 */
	public Universe() {
		planetNames = new String[planetCount];
		planetNames[0] = "Acamar";
		planetNames[1] = "Adahn";
		planetNames[2] = "Aldea";
		planetNames[3] = "Andevian";
		planetNames[4] = "Antedi";
		planetNames[5] = "Balosnee";
		planetArray = new Planet[planetCount];
	}
	
	/**
	 * Generate universe.
	 */
	public void generateUniverse() {
		for(int i=0; i < getPlanetCount(); i++) {
			planetArray[i] = new Planet();
			planetArray[i].setName(planetNames[i]);
		}
	}
	
	/**
	 * Save the string of the planet array
	 * @return saveString
	 */
	public String saveString(){
		String saveString = "";
		saveString += "";
		for(int i = 0; i < planetArray.length; i++){
			saveString += planetArray[i].saveString();
		}
		return saveString;
	}
	
	/**
	 * This is used when loading a previously saved game and reads information from a text file
	 * @param load
	 */
	public void loadString(String load) {
		try {
			String[] input = load.split(",");
			for(int i = 0; i < 6; i++) {
				planetArray[i] = new Planet();
				planetArray[i].setEconomy(Planet.Economy.values()[Integer.parseInt(input[6 * i])]);
				planetArray[i].setGovernment(Planet.Government.values()[Integer.parseInt(input[6 * i + 1])]);
				planetArray[i].setSpecialResource(Planet.SpecialResource.values()[Integer.parseInt(input[6 * i + 2])]);
				planetArray[i].setRadius(Integer.parseInt(input[6 * i + 3]));
				planetArray[i].setXPos(Integer.parseInt(input[6 * i + 4]));
				planetArray[i].setYPos(Integer.parseInt(input[6 * i + 5]));
			}
		}
		catch(Exception e) {
			System.err.println(e);
		}
	}
	
	/**
	 * Draw planets.
	 * @param g2d The graphics object
	 * @param planetLocated The planet the player is on.
	 */
	public void drawPlanets(Graphics2D g2d, Planet planetLocated) {
		for(int i = 0; i < getPlanetCount(); i++) {
			Planet planet = planetArray[i];
			g2d.setPaint(new GradientPaint(planet.getXPos() - planet.getRadius(), planet.getYPos() - planet.getRadius(), planet.getColor(), planet.getXPos() + planet.getRadius(), planet.getYPos() + planet.getRadius(), Color.BLACK));
			g2d.fillOval(planet.getXPos() - planet.getRadius(), planet.getYPos() - planet.getRadius(), 2 * planet.getRadius(), 2 * planet.getRadius());
			if(planet != planetLocated) {
				g2d.setColor(Color.BLACK);
				g2d.drawOval(planet.getXPos() - planet.getRadius(), planet.getYPos() - planet.getRadius(), 2 * planet.getRadius(), 2 * planet.getRadius());
			}
			else {
				g2d.setColor(Color.YELLOW);
				g2d.drawOval(planet.getXPos() - planet.getRadius() - 3, planet.getYPos() - planet.getRadius() - 3, 2 * (planet.getRadius() + 3), 2 * (planet.getRadius() + 3));
				g2d.drawOval(planet.getXPos() - planet.getRadius() - 4, planet.getYPos() - planet.getRadius() - 4, 2 * (planet.getRadius() + 4), 2 * (planet.getRadius() + 4));
			}
		}
	}
	
	/**
	 * Checks which planet the user is clicking on
	 *
	 * @param x the x
	 * @param y the y
	 * @return the int
	 */
	public int checkPlanet(int x, int y) {
		return x;
	}
	
	/**
	 * Gets the planet that the player is currently located on
	 * @param x
	 * @return planetArray[x]
	 */
	public Planet getPlanet(int x) {
		return planetArray[x];
	}
	
	/**
	 * Gets the planet that the player is currently located on
	 * @param x
	 * @return planetArray[x]
	 */
	public Planet getPlanetByName(String x) {
		for(int i = 0; i < planetCount; i++){
			if(planetArray[i].getName().equals(x)){
				return planetArray[i];
			}
		}
		return null;
	}
	
	/**
	 * Converts all planets info into string
	 * 
	 * @return string
	 */
	public String toString() {
		String str = "";
		for(int i = 0; i < getPlanetCount(); i++) {
			str += "Planet " + i + ": " + planetArray[i] + " Tech Level: " + planetArray[i].getEconomy() + '\n' + " Government Type: " + planetArray[i].getGovernment() + '\n' + " Special Resource: " + planetArray[i].getSpecialResource() + '\n';
		}
		return str;
	}

	/**
	 * Gets the amount of planets that are in the universe
	 * @return planetCount
	 */
	public int getPlanetCount() {
		return planetCount;
	}

	/**
	 * Sets the amount of planets that are in the universe
	 * @param planetCount
	 */
	public void setPlanetCount(int planetCount) {
		this.planetCount = planetCount;
	}
}
